package util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import basic.SortInfo;
import utils.StringUtil;

public class ParamUtil implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6366505931304393585L;
	
	public static String tableName(String name) {
		if (null == name || "".equals(name)) {
			return "";
		}
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			String s = name.substring(i, i + 1);
			String slc = s.toLowerCase();
			if (!s.equals(slc)) {
				break;
			} else {
				result.append(s);
			}
		}
		return result.toString();

	}

	public static Map<String, Object> sortMap(Map<String, Object> paramMap, SortInfo sortInfo) {
		if (StringUtil.isEmpty(paramMap)) {
			paramMap = new HashMap<String, Object>();
		}
		if (!StringUtil.isEmpty(sortInfo.getSortField())) {
			String table = tableName(sortInfo.getSortField());
			String order = table + "." + StringUtil.transferUnder(sortInfo.getSortField());
			String asc = ((!StringUtil.isEmpty(sortInfo.getSortOrder()) && sortInfo.getSortOrder())) ? "asc" : "desc";
			paramMap.put(table + "Sort", new Object[] { order, asc });
		}
		return paramMap;
	}

	public static Map<String, Object> polymerizeMap(Map<String, Object> paramMap, String polymerizeInfo) {
		if (StringUtil.isEmpty(paramMap)) {
			paramMap = new HashMap<String, Object>();
		}
		String table = tableName(polymerizeInfo);
		String refer = table + "." + StringUtil.transferUnder(polymerizeInfo);
		paramMap.put(table + "Sum", refer);
		return paramMap;
	}
	

	public static void main(String[] args) {
		String s = tableName("asdmDer0r");
		System.out.println(s);
	}

}
